package com.lx.oms.controller.manager.impl;

import java.math.BigInteger;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lx.oms.common.entity.Query;
import com.lx.oms.common.utils.CommonUtils;
import com.lx.oms.common.utils.Utils;
import com.lx.oms.controller.dao.SysCategoryMapper;
import com.lx.oms.controller.entity.SysCategoryEntity;
import com.lx.oms.controller.manager.SysCategoryManager;

/**
 * 类别
 *
 * @author houzhong

 */
@Component("sysCategoryManager")
public class SysCategoryManagerImpl implements SysCategoryManager {

	@Autowired
	private SysCategoryMapper sysCategoryMapper;

	@Override
	public List<SysCategoryEntity> listCategoryByParentId(Query query) {

		return sysCategoryMapper.listCategoryByParentId(query);
	}

	@Override
	public int saveCategory(SysCategoryEntity category) {
		if (category.getId() == null) {
			category.setId(Utils.id().toString());
		}
		return sysCategoryMapper.save(category);
	}

	@Override
	public SysCategoryEntity getCategoryById(BigInteger id) {

		return sysCategoryMapper.getObjectById(id);
	}

	@Override
	public int updateCategory(SysCategoryEntity category) {

		return sysCategoryMapper.update(category);
	}

	@Override
	public int batchRemoveCategory(String[] id) {

		return sysCategoryMapper.batchRemove(id);
	}

	@Override
	public boolean hasChildren(String[] id) {
		for(String typeId : id) {
			int count = sysCategoryMapper.countCategoryChildren(typeId);
			if(CommonUtils.isIntThanZero(count)) {
				return true;
			}
		}
		return false;
	}

	@Override
	public SysCategoryEntity getCategoryByName(Query query) {
		SysCategoryEntity categoryEntity = sysCategoryMapper.getCategoryByName(query);
		return categoryEntity;
	}

	@Override
	public List<SysCategoryEntity> getCategoryByParentId(Query query) {
		List<SysCategoryEntity> categoryEntity = sysCategoryMapper.getCategoryByParentId(query);
		return categoryEntity;
	}

}
